package com.hg.imajax.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * 自建：数据库连接工具类
 */
public class DataSourceUtil {
	//1、连接数据必要参数
	private static String driver = "com.mysql.jdbc.Driver";
	private static String url="jdbc:mysql://localhost:3306/imdata";
	private static String connect_user ="root";
	private static String connect_pw ="root";
	//2、获取连接
	public static Connection getConnection(){
		Connection con = null;
		try {
			//加载MySql的驱动类   
		     Class.forName(driver);
		     //获取连接
		     con = (Connection) DriverManager.getConnection(url,connect_user,connect_pw);
		} catch (Exception e) {
			 System.out.println("数据库连接异常");
		}
		return con;
	}
	//3、回收资源
	public static void realseSource(Connection con,Statement statement,ResultSet resultSet){
		try{
			try {
				if(resultSet !=null){
					resultSet.close();
				}
			}finally{
				try{
					if(statement !=null){
						statement.close();
					}
				} finally{
					if(con !=null){
						con.close();
					}
				}
			}
		} catch (SQLException e) {
			System.out.println("数据库关闭异常");
		}
	}
	//4、回收资源
	public static void realseSource(Connection con,Statement statement){
		try{
			try{
				if(statement !=null){
					statement.close();
				}
			} finally{
				if(con !=null){
					con.close();
				}
			}
		} catch (SQLException e) {
			System.out.println("数据库关闭异常");
		}
	}
}
